import axios from 'axios'
import router from '@/router'

// create an axios instance
const service = axios.create({
  baseURL: "http://localhost:9999",
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 5000 // request timeout
})

// axios的请求拦截器
service.interceptors.request.use(
  config=>{
    var tokenVal = localStorage.getItem("token");
    // console.log(tokenVal);
    config.headers.token = tokenVal;
    return config;
  }
)

//响应拦截器
service.interceptors.response.use(
  res=>{ //成功回调
    console.log(res);
    if(res.data.code == '403'){
      //跳转到403页面
      router.push({ "path": "/403" })
    }
    return res.data;
  },
  err=>{ //失败回调
    console.log(err);
  }
)

export default service
